package com.company.card_service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.card_service.mapper.CompanyCaseTypeMapper;
import com.company.card_service.mapper.IUserInfoMapper;
import com.company.card_service.mapper.PassageViewMapper;
import com.company.card_service.model.CompanyCaseType;
import com.company.card_service.model.Passage;
import com.company.card_service.mapper.PassageMapper;
import com.company.card_service.model.PassageView;
import com.company.card_service.model.User;
import com.company.card_service.service.PassageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.card_service.vo.PassageFromVo;
import com.company.card_service.vo.PassageQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 赵志浩
 * @since 2022-09-15 04:15:07
 */
@Service
public class PassageServiceImpl extends ServiceImpl<PassageMapper, Passage> implements PassageService {

    @Autowired
    private IUserInfoMapper iUserInfoMapper;

    @Autowired
    private PassageViewMapper passageViewMapper;

    @Override
    public List<PassageFromVo> selectPassage(Integer page, Integer limit, PassageQueryVo passageQueryVo) {
        Page<PassageFromVo> pageParam = new Page<>(page,limit);
        List<PassageFromVo> passageFromVoList = baseMapper.queryPassage(pageParam, passageQueryVo);
        passageFromVoList.stream().forEach(item -> this.getCaseTypeNameAndId(item));
        return passageFromVoList;
    }

    private PassageFromVo getCaseTypeNameAndId(PassageFromVo item) {
        User user = iUserInfoMapper.selectById(item.getSysUserId());
        LambdaQueryWrapper<PassageView> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PassageView::getPassageId,item.getId());
        Long aLong = passageViewMapper.selectCount(wrapper);
        item.setRealView(aLong);
        item.setSysUserId(Integer.parseInt(user.getId().toString()));
        item.setSysUserName(user.getName());
        return item;
    }
}
